<?php

/**
 * @copyright (c) 2016
 * @brief 商品管理类库
 * @author jumark
 * @date 2016/7/21
 * @version 1.0
 */

namespace Home\Model;

use Think\IFilter;
use Think\ITime;
use Think\JSON;

class Goods {
        /**
     * 获取编辑商品所有数据
     * @param int $id 商品ID
     */
    public function edit($id) {
        $id = IFilter::act($id, 'int');
        $goodsWhere = array(
            'id' => $id
        );
        if ($this->seller_id) {
            $goodsWhere['seller_id'] = $this->seller_id;
        }

        //获取商品
        $obj_goods = M('goods');
        $goods_info = $obj_goods->where($goodsWhere)->find();

        if (!$goods_info) {
            return null;
        }

        //赋值到FORM用于渲染
        $data = array('form' => $goods_info);
        
        //品牌
        if($data['form']['brand_id']){
            $data['form']['brand'] = M('brand')->field('name')->find($data['form']['brand_id']);
        }

        //获取货品
        $productObj = M('products');
        $product_info = $productObj->where(array('goods_id' => $id))->select();
        if ($product_info) {
            $data['product'] = $product_info;
        }

        //相册
        $tb_goods_photo = M('goods_photo_relation as ghr');
        $goods_photo_join = 'left join sm_goods_photo as gh on ghr.photo_id=gh.id';
        $goods_photo_fields = 'gh.img';
        $goods_photo_where = 'ghr.goods_id=' . $goods_info['id'];
        $goods_photo_order = 'ghr.id asc';
        $data['goods_photo'] = $tb_goods_photo->join($goods_photo_join)->where($goods_photo_where)->field($goods_photo_fields)->order($goods_photo_order)->select();

        //扩展基本属性
        $goodsAttr = M('goods_attribute');
        $goodsAttr_where = array(
            'goods_id' => $goods_info['id'],
            "attribute_id != ''"
        );
        $attrInfo = $goodsAttr->where($goodsAttr_where)->select();
        if ($attrInfo) {
            foreach ($attrInfo as $item) {
                //key：属性名；val：属性值,多个属性值以","分割
                $data['goods_attr'][$item['attribute_id']] = $item['attribute_value'];
            }
        }

        //商品分类
        $categoryExtend = M('category_extend as cat_e');
        $categoryExtend_where = array(
            'cat_e.goods_id' => $goods_info['id']
        );
        
        $join = "left join sm_category as cat on cat_e.category_id = cat.id";
        $tb_goods_photo_fields = 'cat.name as name,cat.id as id';
        $cateData = $categoryExtend->where($categoryExtend_where)->join($join)->field($tb_goods_photo_fields)->select();
        if ($cateData) {
            foreach ($cateData as $key => $item) {
                $data['goods_category'][$key]['name'] = $item['name'];
                $data['goods_category'][$key]['id'] = $item['id'];
            }
        }
        
        //商品规格
        if($data['form']['spec_array']){
            $spec_arry = JSON::decode($data['form']['spec_array']);
            foreach ($spec_arry as $key => $value) {
                $spec_arry[$key]['value'] = explode(',',$value['value']);
            }
            $data['form']['spec_array'] = $spec_arry;
        }
        return $data;
    }
}
